package com.woniu.sharepark.leasesystem.tenant.service.impl;

import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.dao.UserInfoDAO;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.common.pojo.UserInfo;
import com.woniu.sharepark.constant.SystemConstant;
import com.woniu.sharepark.leasesystem.common.dao.OrderFormDAO;
import com.woniu.sharepark.leasesystem.tenant.dao.CarDAO;
import com.woniu.sharepark.leasesystem.tenant.dao.TenantCommentDAO;
import com.woniu.sharepark.leasesystem.tenant.pojo.TenantComment;
import com.woniu.sharepark.leasesystem.tenant.service.TenantCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *@ClassName TenantCommentImpl
 *@Description 评分接口的实现类
 *@Author litong
 *@Date 2019/6/5 11:55
 *@Version 1.0
 **/
@Service
public class TenantCommentImpl implements TenantCommentService {

    @Autowired
    private TenantCommentDAO tenantCommentDAO;
    @Autowired
    private OrderFormDAO orderFormDAO;
    @Autowired
    private CarDAO carDAO;
    @Autowired
    private UserInfoDAO userInfoDAO;

    @Override
    public List<TenantComment> findAllComments(Integer currentPage,Integer pageSize,Integer oId) {
        int carId=orderFormDAO.selectByPrimaryKey(oId).getCarId();
        int uIdb=carDAO.selectByPrimaryKey(carId).getuId();
       List<TenantComment>list= tenantCommentDAO.queryAllComments(currentPage,pageSize,uIdb);
       for (TenantComment tenantComment:list){
           int uIdb1=tenantComment.getuIdb();
           String ubName=userInfoDAO.selectUNameByUId(uIdb1);
           UserInfo bcomment=new UserInfo();
           bcomment.setuName(ubName);
           tenantComment.setBcomment(bcomment);
           int uIdc=tenantComment.getuIdc();
           String ucName=userInfoDAO.selectUNameByUId(uIdc);
           UserInfo ccomment=new UserInfo();
           ccomment.setuName(ucName);
           tenantComment.setCcomment(ccomment);
       }
       return list;
    }

    @Override
    public JsonResult addComment(TenantComment tenantComment,Integer oId) {
        int carId=orderFormDAO.selectByPrimaryKey(oId).getCarId();
        int uIdb=carDAO.selectByPrimaryKey(carId).getuId();
        Session session = SecurityUtils.getSubject().getSession();
        Login login = (Login) session.getAttribute("currentUser");
        int uIdc=login.getUserInfo().getuId();
        tenantComment.setuIdc(uIdc);
        tenantComment.setuIdb(uIdb);
        tenantCommentDAO.addComment(tenantComment);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult deleteCommentById(Integer tcId) {
        tenantCommentDAO.deleteById(tcId);
        return JsonResult.ok("删除成功");
    }

    @Override
    public JsonResult allComments(Integer oId) {
        int carId=orderFormDAO.selectByPrimaryKey(oId).getCarId();
        int uIdb=carDAO.selectByPrimaryKey(carId).getuId();
        List<TenantComment>list= tenantCommentDAO.queryAllTenantComments(uIdb);
        for (TenantComment tenantComment:list){
            int uIdb1=tenantComment.getuIdb();
            String ubName=userInfoDAO.selectUNameByUId(uIdb1);
            UserInfo bcomment=new UserInfo();
            bcomment.setuName(ubName);
            tenantComment.setBcomment(bcomment);
            int uIdc=tenantComment.getuIdc();
            String ucName=userInfoDAO.selectUNameByUId(uIdc);
            UserInfo ccomment=new UserInfo();
            ccomment.setuName(ucName);
            tenantComment.setCcomment(ccomment);
        }
        return new JsonResult("Success", SystemConstant.SUCCESS_CODE,list);
    }
}
